let baseUrl = "https://api-hmugo-web.itheima.net/api/public/v1/";
// 同时请求的次数
let ajaxtimes = 0;
export const request = (params) => {
  // 把token封装到请求头
  let header = {...params.url}
  if (params.url.includes('my/')) {
    header['Authorization'] = wx.getStorageSync('token')
  }

  ajaxtimes++;
  wx.showLoading({
    title: '加载中',
    mask: true,
  });
  return new Promise((resolve, reject) => {
    wx.request({
      ...params,
      header: header,
      url: baseUrl + params.url,
      success: (res) => {
        resolve(res.data.message)
        wx.stop
      },
      fail: (err) => {
        reject(err)
      },
      // 不管成功失败都触发
      complete: () => {
        ajaxtimes--
        // 所有请求完了关闭
        if (ajaxtimes == 0) {
          wx.hideLoading()
        }
      }
    })
  })
}